//package com.fangzhi.task;
//
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.fangzhi.mobel.AdminAccountInfo;
//import com.fangzhi.mobel.Commission;
//import com.fangzhi.mobel.OrderProducts;
//import com.fangzhi.mobel.Orders;
//import com.fangzhi.service.AdminAccountInfoService;
//import com.fangzhi.service.CommissionService;
//import com.fangzhi.service.OrderProductsService;
//import com.fangzhi.service.OrdersService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.PostConstruct;
//import java.time.LocalDate;
//import java.time.YearMonth;
//import java.util.Arrays;
//import java.util.List;
//import java.util.stream.Collectors;
//
//@Component
//public class CommissionTask {
//
//    @Autowired
//    private OrdersService ordersService;
//    @Autowired
//    private OrderProductsService orderProductsService;
//    @Autowired
//    private AdminAccountInfoService adminAccountInfoService;
//    @Autowired
//    private CommissionService commissionService;
//
//    /**
//     * 一小时运行一次
//     * 更新订单佣金
//     */
//    @Scheduled(cron = "0 0/10 * * * ? ")
////    @PostConstruct
//    public void start(){
//        LocalDate start = YearMonth.now().atDay(1);
//        LocalDate end = start.withDayOfMonth(start.lengthOfMonth());
//        List<AdminAccountInfo> list = adminAccountInfoService.list();
//        for (AdminAccountInfo adminAccountInfo : list) {
//            List<Orders> ordersList = ordersService.list(Wrappers.<Orders>lambdaQuery()
//                    .eq(Orders::getAdminAccountId, adminAccountInfo.getId())
//                    .between(Orders::getCtime, start, end)
//                    .in(Orders::getStatus, Arrays.asList(3, 4, 5)));
//            if (ordersList.isEmpty()){
//                Commission one = commissionService.getOne(Wrappers.<Commission>lambdaQuery()
//                        .eq(Commission::getAccountId, adminAccountInfo.getId()).eq(Commission::getMonth, YearMonth.now().atDay(1)));
//                if (null == one)
//                    commissionService.saveOrUpdate( new Commission().setAccountId(adminAccountInfo.getId())
//                            .setMonth(YearMonth.now().atDay(1)).setCommissionPrice(0).setPrice(0));
//                continue;
//            }
//
//            List<String> collect = ordersList.stream().map(T -> T.getOrderId()).collect(Collectors.toList());
//            List<OrderProducts> orderProductsList = orderProductsService
//                    .list(Wrappers.<OrderProducts>lambdaQuery().in(OrderProducts::getOrderId, collect));
//            Integer total = ordersList.stream().mapToInt(T -> T.getTotalAmount() - T.getDiscountAmount()).sum();
//            Integer premium = orderProductsList.stream().mapToInt(T -> T.getPremiumPrice() * T.getNum()).sum();
//            Integer commission = getCommission(total / 100, premium / 100);
//            Commission one = commissionService.getOne(Wrappers.<Commission>lambdaQuery()
//                    .eq(Commission::getAccountId, adminAccountInfo.getId()).eq(Commission::getMonth, YearMonth.now().atDay(1)));
//            if (null == one){
//                one = new Commission().setAccountId(adminAccountInfo.getId())
//                        .setMonth(YearMonth.now().atDay(1)).setCommissionPrice(commission).setPrice(total);
//            }else{
//                one = one.setAccountId(adminAccountInfo.getId())
//                        .setMonth(YearMonth.now().atDay(1)).setCommissionPrice(commission).setPrice(total);
//            }
//            commissionService.saveOrUpdate(one);
//        }
//    }
//
//
//    public static Double t1 = 0.05;
//    public static Double t2 = 0.07;
//    public static Double t3 = 0.09;
//    public static Double t4 = 0.13;
//    public static Double t5 = 0.15;
//    public static Double t0 = 0.5;
//    public Integer getCommission(Integer total, Integer premium){
//        Double ticheng = 0D;
//        double yijiaticheng = premium * t0;
//        if (total > 150000){
//            ticheng += 30000 * t1;
//            ticheng += 30000 * t2;
//            ticheng += 50000 * t3;
//            ticheng += 30000 * t4;
//            ticheng += (total - 150000) * t5;
//            ticheng += yijiaticheng;
//        } else if (total > 130000){
//            ticheng += 30000 * t1;
//            ticheng += 30000 * t2;
//            ticheng += 50000 * t3;
//            ticheng += (total - 130000) * t4;
//            ticheng += yijiaticheng;
//        }else if (total > 80000){
//            ticheng += 30000 * t1;
//            ticheng += 30000 * t2;
//            ticheng += (total - 80000) * t3;
//            ticheng += yijiaticheng;
//        }else if (total > 50000){
//            ticheng += 30000 * t1;
//            ticheng += (total - 50000) * t2;
//            ticheng += yijiaticheng;
//        }else if (total > 20000){
//            ticheng += (total - 20000) * t1;
//            if ((total - premium) > 20000){
//                ticheng += yijiaticheng;
//            }else {
//                ticheng += (total - 20000) * t0;
//            }
//        }
//        ticheng = ticheng * 100;
//        return ticheng.intValue();
//    }
//}
